﻿using System;
using System.Collections.Generic;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.Split;
using NTTDATA.BEENChina.ResultCertification.model;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Address
{
   
    [Serializable]
    [VerfityAttribute(Id = "VerfityA0002", Name = "县市检证", CategoryType = EnumCategoryType.住所, Model = true)]
    public class VerfityA0002 : VerfityOneResult
    {

 
        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (result == null)
            {
                return false;
            }

            if (entryCommon == "30" || entryCommon == "20")
            {
                return false;
            }

            foreach (List<string> rowResult in result)
            {

                foreach (string colReulst in rowResult)
                {
                  
                    bresult = CheckPrefectureAndCity(colReulst);

                    if (bresult)
                    {
                        break;
                    }
                }

                if (bresult)
                {
                    break;
                }

            }
            
           

            if (bresult)
            {
                System.Console.WriteLine("VerfityA0002");
            }

            return bresult;
        }

        /// <summary>
        /// 县市组合不存在于检索库中时抽出(
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool CheckPrefectureAndCity(string result)
        {
            bool bresult = false;


            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, result);

            string prefecture = splitResult.GetPrefecture();
            string cityDistrict = splitResult.GetCity();

            string city = SplitAddressResult.GetCity(cityDistrict);
            string district = SplitAddressResult.GetDistrict(cityDistrict);

            string pId = string.Empty;

            //县，市 都没有记载 不抽出
            if (string.IsNullOrEmpty(prefecture) &&
                string.IsNullOrEmpty(cityDistrict))
                return false;

            if (!String.IsNullOrEmpty(prefecture))
            {
                //县
                bresult = Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                    t => t.Type.Equals("p") &&
                         t.Name.Equals(prefecture));

              
                if (!bresult)
                {
                    //没找到县
                    return true;
                }
                
                //得到县Id
                pId = Logic.JaCitys.GetInstance().Citys.AsParallel().Single(t => t.Type.Equals("p") && t.Name.Equals(prefecture)).ID;
                
            }

            //市判断结束
        
                //县市
                if (!String.IsNullOrEmpty(city))
                {
                    bresult =
                        Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                            t =>
                                {
                                    //if (t.ID.Equals("05071"))
                                    //    Console.WriteLine("05071");

                                    bool checkCity ;
                                    if (String.IsNullOrEmpty(pId))
                                    {
                                         checkCity =   t.Type.Equals("c") && t.Name.Equals(city);
                                    }
                                    else
                                    {
                                        checkCity = t.Type.Equals("c") && t.Name.Equals(city) && t.ParentID.Equals(pId);
                                    }
                                    return checkCity;
                                });

                    if (bresult)
                    {


                      var citys =  Logic.JaCitys.GetInstance().Citys.AsParallel().Where(
                            t => t.Type.Equals("c") && t.Name.Equals(city)).ToList();


                      if (!city.Equals(district))
                      {
                          bresult = citys.Any(
                              jaCityItem =>
                                  {
                                      string cId = jaCityItem.ID;

                                      if (!String.IsNullOrEmpty(district))
                                      {
                                          //县市区
                                          return Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                                              t =>
                                              t.Type.Equals("d") && t.Name.Equals(district) && t.ParentID.Equals(cId));

                                      }
                                      return true;
                                  }
                              );

                          if (!bresult)
                          {
                              //没找到区
                              return true;
                          }
                      }
                        //foreach (JaCityItem jaCityItem in citys)
                        //{
                        //    string cId = jaCityItem.ID;

                        //    if (!String.IsNullOrEmpty(district))
                        //    {
                        //        //县市区
                        //        bresult = Logic.JaCitys.GetInstance().Citys.AsParallel().Any(
                        //            t => t.Type.Equals("d") && t.Name.Equals(district) && t.ParentID.Equals(cId));

                        //        if (!bresult)
                        //        {
                        //            //没找到区
                        //            return true;
                        //        }
                        //    }
                        //}
                       
                    }
                    else
                    {
                        //没找到市
                        return true;
                    }
                }
                else if (!String.IsNullOrEmpty(district))
                {
                    //没有市 有区
                    return false;
                }
          
           

            return false;
        }
    }
}
